<?php

namespace app\beautiful\controller\admin\setting;

use app\beautiful\model\StaffModel;
use app\BaseController;
use app\common\logic\DataLogic;
use app\common\model\CustomerServiceModel;
use app\common\model\ShopBrandModel;
use app\common\model\ShopDetailModel;
use app\common\model\ShopImgModel;
use app\common\model\ShopModel;

class Shop extends BaseController{

    public function index()

    {
        $search = $this->request->param('search');

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0]

        ];

        if(!empty($search)){
            $where[] = ['name|mobile','like','%'.$search.'%'];
        }

        $staff = new StaffModel();
        $staff = $staff->withCount(['technician'=>function($query){

            $query->where([

                ['shop_id','=',$this->request->shopId],

                ['is_delete','=',0]

            ]);

        }]);

        $order=['add_time'=>'desc'];

        $field='*';

        $append=['jurisdiction','add_time_format'];

        $return = DataLogic::getDataList($staff,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        foreach ($return['list'] as $k=>$v){

            $return['list'][$k]['is_bind'] = ($v['technician_count']>0)?1:0;
        }

        $return['miniapp_status'] = $this->request->shop->status;

        return $this->success($return);

    }
    public function info(){
        $shop_id=$this->request->shopId;
        $where['shop_id']=$shop_id;
        $info=ShopModel::info($where);
        return $this->success($info);
    }
    public function brandlist(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $parent_id = $this->request->param('parent_id',0);
        $where=[];
        $order['sort']='desc';
        $order['id']='desc';
        $where['parent_id']=$parent_id;
        $res_data=ShopBrandModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function up(){
        $shop_id=$this->request->shopId;
        $name = $this->request->param('name',null);
        $brand_id=$this->request->param('brand_id',null);
        $logo=$this->request->param('logo',null);
        $qrcode=$this->request->param('qrcode',null);
        $mobile=$this->request->param('mobile',null);
        $wx_account=$this->request->param('wx_account',null);
        $city=$this->request->param('city',null);
        $address=$this->request->param('address',null);
        $lng=$this->request->param('lng',null);
        $lat=$this->request->param('lat',null);
        $business_time=$this->request->param('business_time',null);
        $per_money=$this->request->param('per_money',null);
        $is_member=$this->request->param('is_member',null);
        $info=$this->request->param('info',null);
        $imgurl=$this->request->param('imgurl',null);
        $is_cover=$this->request->param('is_cover',null);
        $img_del=$this->request->param('img_del',null);
        $img_id=$this->request->param('img_id',null);
        $customer_service_id=$this->request->param('customer_service_id',null);
        $shop_up=[];
        $detail_up=[];
        $cus_up=[];
        $img_up=[];
        if($name!==null) $shop_up['name']=$name;
        if($brand_id!==null) $shop_up['brand_id']=$brand_id;
        if($logo!==null) $detail_up['logo']=$logo;
        if($address!==null) $detail_up['address']=$address;
        if($city!==null) $detail_up['city']=$city;
        if($lng!==null) $detail_up['lng']=$lng;
        if($lat!==null) $detail_up['lat']=$lat;
        if($business_time!==null) $detail_up['business_time']=json_encode($business_time);
        if($per_money!==null) $detail_up['per_money']=$per_money;
        if($is_member!==null) $detail_up['is_member']=$is_member;
        if($info!==null) $detail_up['info']=$info;
        if($qrcode!==null) $cus_up['qrcode']=$qrcode;
        if($mobile!==null) $cus_up['mobile']=$mobile;
        if($wx_account!==null) $cus_up['wx_account']=$wx_account;
        if($imgurl!==null) $img_up['imgurl']=$imgurl;
        if($is_cover!==null) $img_up['type']=$is_cover;
        if(!empty($detail_up['lng']) && !empty($detail_up['lat'])){
            $url = "https://apis.map.qq.com/ws/coord/v1/translate";
            $url = $url."?key=LANBZ-MSKK4-Q2FUB-XCOEP-KB4V5-RDFCV&type=3&locations={$detail_up['lat']},{$detail_up['lng']}";
            $res = https_curl($url,[]);
            if(empty($res['status'])){
                $detail_up['t_lng']=$res['locations'][0]['lng'];
                $detail_up['t_lat']=$res['locations'][0]['lat'];
            }
        }
        if(!empty($shop_up)){
            ShopModel::up($shop_up,['shop_id'=>$shop_id]);
        }
        if(!empty($detail_up)){
            ShopDetailModel::up($detail_up,['shop_id'=>$shop_id]);
        }
        if(!empty($cus_up)){
            if(!empty($customer_service_id)){
                CustomerServiceModel::up($cus_up,['customer_service_id'=>$customer_service_id]);
            }else{
                $cus_up['add_time']=time();
                $cus_up['add_ip']=$this->request->ip();
                $customer_service_id=CustomerServiceModel::insertGetId($cus_up);
                ShopModel::up(['customer_service_id'=>$customer_service_id],['shop_id'=>$shop_id]);
            }
        }
        if(!empty($img_up)){
            if(empty($img_id)){
                $img_up['shop_id']=$shop_id;
                $img_up['create_time']=date('Y-m-d H:i:s');
                ShopImgModel::insert($img_up);
            }else{
                if($img_del==1){//删除图片
                    ShopImgModel::del(['id'=>$img_id]);
                }else{
                    if($img_up['type']==1){
                        ShopImgModel::up(['type'=>0],['shop_id'=>$shop_id]);
                    }
                    ShopImgModel::up($img_up,['id'=>$img_id]);
                }

            }
        }
        return $this->success();
    }
    public function voice_up(){
        $shop_id=$this->request->shopId;
        $sbx_id = $this->request->param('sbx_id',null);
        if($sbx_id!==null){
            //是否已被绑定
            $data = ShopDetailModel::getById(['sbx_id'=>$sbx_id]);
            if($data){
                $shop = ShopModel::find($data['shop_id']);
                if($data && $data['shop_id']!=$shop_id && $shop) return $this->error('已被门店'.$shop->name.'绑定');
            }
            $detail_up['sbx_id']=$sbx_id;
            ShopDetailModel::up($detail_up,['shop_id'=>$shop_id]);
            return $this->success();
        }else{
            return $this->error('参数错误');
        }
    }
    public function voice_del(){
        $shop_id=$this->request->shopId;
        $detail_up['sbx_id']='';
        ShopDetailModel::up($detail_up,['shop_id'=>$shop_id]);
        return $this->success();
    }
    public function voice_test(){
        $sbx_id = $this->request->param('sbx_id',null);
        $test_content=$this->request->param('test_content',null);
        if($sbx_id!==null && $test_content!==null){
            $test['msg']=$test_content;
            $test['sbx_id']=$sbx_id;
           $res=sendvoice($test);
           if($res['result']=='success'){
               return $this->success();
           }else{
               return $this->error($res['msg']);
           }
        }else{
            return $this->error('参数错误');
        }
    }
}

